package javaEmail;

import java.io.InputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class test {

	public static void main(String[] args) throws MessagingException {
		 String host ="smtp.qq.com";
		   int port = 587;
		   String userName = "123456";
		   String password = "XXXXXXXXXXXXXXX";
		   String affix = ""; //附件地址
		   String affixName = ""; //附件名称
		    Properties javaMailProperties = new Properties();
		   
		   javaMailProperties.put("mail.smtp.auth", "true");
		    javaMailProperties.put("mail.smtp.starttls.enable", "true");
		    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		    mailSender.setHost(host);
		        mailSender.setPort(port);
		        mailSender.setUsername(userName);
			        mailSender.setPassword(password);
			        mailSender.setJavaMailProperties(javaMailProperties);
			     
		            String nickname = "猫的";		         
		            DataSource dataSource1=new FileDataSource("d:/a3.jpg");
			        MimeMessage message = mailSender.createMimeMessage();
			        MimeMessageHelper help = new MimeMessageHelper(message, true, "UTF-8");
			        help.setFrom("91345379052@qq.com");
		 	        help.setTo("7654@qq.com");
		            help.setSubject("测试");
		            help.addAttachment("a3.jpg", dataSource1);
			        String content = "你好" + nickname;     
			        help.setText(content, true);
			     
			        System.out.println("邮件已发送");
			        
			  
			        try {
			            mailSender.send(message);
			        } catch (Throwable e) {
			            e.printStackTrace();
			        }
		}}
